package org.wedding.config.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }

    /**
     * 优惠卷key 过期超时监听
     * @param container
     * @return
     */
    @Bean
    public CouponKeyExpirationListener couponKeyExpirationListener(RedisMessageListenerContainer container) {
        // 配置监听器只监听 `coupon:*` 键
        CouponKeyExpirationListener listener = new CouponKeyExpirationListener(container);
        container.addMessageListener(listener, new ChannelTopic("__keyevent@0__:expired"));
        return listener;
    }

    @Bean
    public OrderKeyExpirationListener orderKeyExpirationListener(RedisMessageListenerContainer container) {
        // 配置监听器只监听 `order:*` 键
        OrderKeyExpirationListener listener = new OrderKeyExpirationListener(container);
        container.addMessageListener(listener, new ChannelTopic("__keyevent@0__:expired"));
        return listener;
    }

}
